<?php
//check1
namespace Home\Controller;
use Think\Controller;
use Think\Model;
use Home\Model\AotherModel;

class AotherController extends Controller {
    public function index(){
   		if(session('?user')){
   			$this->assign('user',I('session.user'));
    		$reserve = M('Areferences');
    		$data1['email'] = I('session.user');
    		$data1['submit'] = '0';
    		$data2['email'] = I('session.user');
    		$data2['submit'] = '1';
    		$result1=$reserve->where($data1)->select();
    		$result2=$reserve->where($data2)->select();
    		if($result1){
    			$this->assign('data', $result1[0]);
    			$this->display();
    		}
    		else if($result2)
    			redirect(U('Myhomepage/index'));  		
    		else{
    			redirect(U('Student/index'));
    		}
    	} 	
    	else
    		redirect(U('Login/index'));
    }
    public function back(){
    	if(session('?user')){
    		$this->assign('user',I('session.user'));
    		redirect(U('Areferences/index'));
    	}
    	else
    		redirect(U('Login/index'));
    }
    public function save(){
   		if(IS_POST&&session('?user')){
	    	$table = D('Aother');
	    	$data['email']=session('user');
	    	$data['default'] = I('post.default');
	    	$data['criminal_record'] = I('post.criminal_record');
	    	$data['bankrupt'] = I('post.bankrupt');	    	
	    	if ($data1=$table->create($data)){
	    		if($table->add($data1)){
	    			$update['email']=session('user');
	    			$update['submit'] = '1';
	    			$table1 = M('Abasics');
	    			$table2 = M('Aparents');
	    			$table3 = M('Acontact');
	    			$table4 = M('Aeducation');
	    			$table5 = M('Aemployment');
	    			$table6 = M('Afinancials');
	    			$table7 = M('Areferences');
	    			$table1->save($update);
	    			$table2->save($update);
	    			$table3->save($update);
	    			$table4->save($update);
	    			$table5->save($update);
	    			$table6->save($update);
	    			$table7->save($update);
	    			redirect(U('Myhomepage/index'));
	    		}
	    		else{
	    			$error=$table->getError();
	    			$this->assign('message','保存失败');
	    			$this->assign('title1','保存失败');
	    			$this->assign('pagename','申请页面');
	   				$this->assign('data',$error);
	   				$this->error('保存失败！','index',3);	    		
	    		}    		
	    	}
	    	else{
	    		$error=$table->getError();
	    		$this->assign('message','保存失败');
	    		$this->assign('title1','保存失败');
	    		$this->assign('pagename','申请页面');
	    		$this->assign('data',$error);
	    		$this->error('保存失败！','index',3);
	    	} 
    	}
    	else
    		$this->display('index');  		    	
    }
}